package com.example.testapplication;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * @author wwq on 2019/7/24  13:13.
 */
public class ArticleRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    View view;
    private OnItemClickListener onItemClickListener;


    public void setOnClick(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        ViewHolder(View v) {
            super(v);
            title = v.findViewById(R.id.title);
        }
    }

    private List<Question> questionList;

    ArticleRecyclerViewAdapter(List<Question> list) {
        this.questionList = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_article, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
        Question question = questionList.get(position);
        ((ViewHolder) holder).title.setText(question.getTitle());
        final ImageView ivDelete = view.findViewById(R.id.ivDelete);
        ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, position);
                }
//                Log.e("TAG", String.valueOf(pos));
//                AlertDialog.Builder builder = new AlertDialog.Builder(view1.getContext());
//                builder.setTitle("删除后，不可恢复，确定删除吗？");
//                builder.setMessage("111");
//                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                    }
//                });
//                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        removeData(pos);
//                    }
//                });
            }
        });
    }


//    public int getPosition(){
//        return mRecyclerView.getChildAdapterPosition(view);
//    }

    @Override
    public int getItemCount() {
        return questionList.size();
    }

    public void removeData(int position) {
        if (position >= 0 && position < questionList.size()) {
            questionList.remove(position);
            notifyItemRemoved(position);
//           notifyItemRangeChanged(position, getItemCount());
            notifyDataSetChanged();
        }
    }
}
